package crud;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;

import entities.ExamineDetail;
import entities.PatientDetail;
import entities.User;

public class PatientDetailCRUD extends AbstractGRUD<PatientDetail> {

	public PatientDetailCRUD() {
		super(PatientDetail.class);
	}

	// public String create_Patient_Examine(String address, String emgContact,
	// String firstName,
	// String lastName, Date appointmentDay) {
	// EntityManager em = this.getEm();
	// String result = "success";
	// try {
	// PatientDetail patient = new PatientDetail(address, emgContact, firstName,
	// lastName);
	// ExamineDetail examine = new ExamineDetail(appointmentDay, patient);
	// em.getTransaction().begin();
	// em.persist(patient);
	// int a = patient.getIdPatientDetail();
	// em.persist(examine);
	// em.getTransaction().commit();
	// } catch (Exception e) {
	// result = e.getMessage();
	// em.getTransaction().rollback();
	// }
	// return result;
	// }
	

	public String create_Patient_Examine_User(String address, String emgContact, String firstName,
			String lastName, Date appointmentDay) {

		EntityManager em = this.getEm();
		String result = "success";
		try {

			em.getTransaction().begin();

			String password = Integer.toString(100000 + (int) (Math.random() * 899999));
			String idUser = "";
			List<PatientDetail> patientDetails = readByQuery("SELECT p FROM PatientDetail p WHERE p.idPatientDetail =(SELECT MAX(p2.idPatientDetail) FROM PatientDetail p2) ");
			if (patientDetails.isEmpty()) {
				idUser = "PA00000";
			} else {
				int idPatient = patientDetails.iterator().next().getIdPatientDetail();
				idUser = "PA" + Integer.toString(100001 + idPatient).substring(1);
			}

			User user = new User(idUser, password, "user");
			PatientDetail patient = new PatientDetail(address, emgContact, firstName, lastName, user);
			ExamineDetail examine = new ExamineDetail(appointmentDay, patient);
			
		List<ExamineDetail> examineDetails = new ArrayList<ExamineDetail>();
		examineDetails.add(examine);
		patient.setExamineDetails(examineDetails);

			em.persist(user);
			em.persist(patient);
			em.persist(examine);
			em.getTransaction().commit();
			

		} catch (Exception e) {
			result = e.getMessage();
			em.getTransaction().rollback();

		}

		return result;

	}

	public String delete_NoneExamine_Patient(int idPatientDetail) {
		EntityManager em = this.getEm();
		String result = "success";
		try {
			PatientDetail patient = em.find(PatientDetail.class, idPatientDetail);
			User user;
			if (patient != null) {
				user = patient.getUser();
				List<ExamineDetail> examineDetails = patient.getExamineDetails();
				if (examineDetails.isEmpty()) {
					em.getTransaction().begin();
					if (user != null) {
						em.remove(user);
					}
					em.remove(patient);
					em.getTransaction().commit();
				} else if (examineDetails.size() == 1) {
					ExamineDetail examineDetail = examineDetails.iterator().next();
					if (examineDetail.getExamineDay() == null) {
						em.getTransaction().begin();
						em.remove(examineDetail);
						em.remove(patient);
						if (user != null) {
							em.remove(user);
						}
						em.getTransaction().commit();
					}
				}

			} else {
				result = "fail";
			}

		} catch (Exception e) {
			result = e.getMessage();
			em.getTransaction().rollback();
		}
		return result;
	}
}
